package com.aliyun.messagemgt.web.controller.push;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson.JSON;
import com.aliyun.messagemgt.common.dto.send.MsgPushDTO;
import com.aliyun.messagemgt.application.service.FileUpload;
import com.aliyun.messagemgt.application.service.MsgRecordService;
import com.aliyun.messagemgt.application.service.PushService;
import com.aliyun.messagemgt.common.dto.send.SystemMsgPushDTO;
import com.aliyun.messagemgt.util.redis.Redis;
import com.aliyun.messagemgt.dto.in.ApiStatisticsQuery;
import com.aliyun.messagemgt.dto.in.MsgQuery;
import com.aliyun.messagemgt.feignapi.PushAPIHttp;
import com.aliyun.messagemgt.util.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * Created with IDEA
 *
 * @author:zhangxingxing 0027012120
 * @date:2019/1/22
 * @time:17:36
 */
@RestController
public class PushController implements PushAPIHttp {

    @Autowired
    private PushService pushService;
    @Autowired
    private FileUpload fileUpload;
    @Autowired
    private MsgRecordService msgRecordService;

    private static final Logger logger = LoggerFactory.getLogger(PushController.class);

//    /**
//     * 简单人工推送【2020年7月7日16:00:37】
//     *
//     * @param msgRecordVO
//     * @return
//     */
//    @Override
//    public Object push(@RequestBody MsgRecordVO msgRecordVO) {
//        pushService.simplePush(msgRecordVO);
//        return msgRecordVO;
//    }

    /**
     * 图片上传oss返回url
     *
     * @param
     * @return
     */
    @Override
    public Object pictureToUrl(@RequestParam MultipartFile file) {
        return fileUpload.pictureToUrl2(file);
    }

//    /**
//     * 对外推送接口
//     *
//     * @param
//     * @return
//     */
//    @Override
//    public Object msgPush(@Valid @RequestBody MsgPushVO msgPushVO, BindingResult bindingResult) {
//        pushService.apiPush(msgPushVO);
//        return msgPushVO.getMsgList();
//    }

    /**
     * 消息接收及对外推送接口
     *
     * @param msgPushDTO
     * @return
     */
    @Override
    public Response msgPush(MsgPushDTO msgPushDTO) {
        return pushService.apiPush(msgPushDTO);
    }

    /**
     * 应用端消息接收
     *
     * @param systemMsgPushDTO
     * @return
     */
    @Override
    public Object systemCsbMsgReceive(SystemMsgPushDTO systemMsgPushDTO) {
        HttpServletRequest request = SpringContextUtils.getRequest();
        logger.info("appKey:{},appSecret:{},systemMsgPushDTO:{}",request.getHeader("appKey"),request.getHeader("appSecret"),JSON.toJSONString(systemMsgPushDTO));
        return SingleResponse.of(systemMsgPushDTO);
    }

    /**
     * 对外历史消息查询接口(分页)
     *
     * @param
     * @return
     */
    @Override
    public Object listHistory(@RequestBody MsgQuery msgQuery) {
        return null;
    }

    /**
     * 给门户系统提供消息中心各种渠道的统计数据
     *
     * @param apiStatisticsQuery 查询类
     * @return object
     */
    @Override
    public Object statisticsApi(@Valid @RequestBody ApiStatisticsQuery apiStatisticsQuery) {
        return msgRecordService.queryMsgRecordsStatistics(apiStatisticsQuery);
    }

    /**
     * 接收消息测试接口
     *
     * @param
     * @return
     */
    @Override
    public Object receiveTest(@RequestBody Map map) {
        logger.info("receiveTest {}", map);
        return JSON.toJSONString(map);
    }

    @Override
    public Object redisTest(@RequestBody Map map) {
        logger.info("redisTest {}", map);
        return Redis.getValue((String) map.get("key"));
    }

}
